// pagesFilterDicSelect/pagesFilterDicSelect.js 
const app = getApp()
import {
  convertToTree,
  dicToConvert
} from '../../../../utils/util.js';
Component({
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    dicKey: {
      type: String,
      value: ''
    },
    value: {
      type: String,
      value: ''
    },
    multiple: {
      type: Boolean,
      value: false,
    },
  },
  /**
   * 页面的初始数据
   */
  data: {
    // 要返回的页面路径
    backToPath: '',
    dataList: [],
    sourceData: [],
    copyViewListData: [],
    viewListData: [],
    globalDataKey: ''
  },
  lifetimes: {
    ready() {
      const {
        dicKey
      } = this.data
      if (dicKey) {
        const findValue = app.globalData.dicList.find((item) => item.type === dicKey)

        if (findValue) {
          const sourceData = dicToConvert(findValue.data)
          const treeList = convertToTree(sourceData, {
            id: 'id',
            parentId: 'parentId',
            name: 'dictLabel'
          })
          const copyViewListData = treeList
          const viewListData = treeList
          this.setData({
            sourceData: sourceData,
            copyViewListData: copyViewListData,
            viewListData: viewListData,
          })
        }

      }
    }
  },
  methods: {


    // 确认
    onConfirm({
      detail
    }) {
      this.setData({
        visible: false,
      })
      this.triggerEvent('onConfirmDicSelectPopup', {
        value: detail.value[0].value,
        valueLabel: detail.value[0].name
      })

    },
    // 取消
    onCancel() {
      this.setData({
        visible: false
      })
    },

  }

})